package com.gw.mvc.unit;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gw.mvc.bean.ConfigBean;

public class ParseXml {

	public static Map<String,ConfigBean> getConfigByDom4j(String filePath){
		Map<String,ConfigBean> configMap = new HashMap<String,ConfigBean>();
		SAXReader r = new SAXReader();
		Document document = null; 
		try{
			document =	r.read(new File(filePath));
			Element root = document.getRootElement();
			
			Map<String,String> beanMap = new HashMap();
			List<Element> beanList = root.selectNodes("formBeans/bean");
			for( Element bean : beanList ){
				String name  = bean.attributeValue("name");
				String clazz = bean.attributeValue("class");
				beanMap.put( name , clazz );
			}
			
			List<Element> actionList = root.selectNodes("action-mapping/action");
			for( Element action : actionList ){
				String name = action.attributeValue("name");
				String type = action.attributeValue("type");
				String path = action.attributeValue("path");
				List<Element> forwList = action.selectNodes( "forward" );
				Map<String,String> forwMap = new HashMap();
				for( Element forw : forwList ){
					String fName  = forw.attributeValue( "name" );
					String fValue = forw.attributeValue( "value" );
					forwMap.put(fName, fValue);
				}
				ConfigBean conf = new ConfigBean( name , beanMap.get(name) ,
						type , path , forwMap );
				configMap.put( path , conf );
			}
			
		}catch(Exception exp){
			exp.printStackTrace();
			System.out.println( ">>>> parse xml Error ...! " );
		}
		return configMap;
	}
	
	public static Map<String,ConfigBean> getConfigByJdom(String filePath){
		Map<String,ConfigBean> configMap = new HashMap<String,ConfigBean>();
		return configMap;
	}
}
